import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;


public class ColorMap {
	
	protected static float map[][] = null;
	/*protected static float map[][] = { 
								{1.00F, 1.00F, 1.00F},
								{1.00F, 1.00F, 1.00F},
								{1.00F, 1.00F, 1.00F},
								{1.00F, 1.00F, 1.00F},
								{1.00F, 1.00F, 1.00F},
								{1.00F, 1.00F, 1.00F},
								{1.00F, 1.00F, 1.00F},
								{1.00F, 1.00F, 1.00F},
								{1.00F, 1.00F, 1.00F},
            					{0.90F, 0.90F, 0.90F},
								{0.90F, 0.90F, 0.90F},
								{0.90F, 0.90F, 0.90F},
								{0.90F, 0.90F, 0.90F},
								{0.90F, 0.90F, 0.90F},
								{0.90F, 0.90F, 0.90F},
								{0.90F, 0.90F, 0.90F},
								{0.90F, 0.90F, 0.90F},
								{0.90F, 0.90F, 0.90F},
            					{0.80F, 0.80F, 0.80F},
            					{0.80F, 0.80F, 0.80F},
            					{0.80F, 0.80F, 0.80F},
            					{0.80F, 0.80F, 0.80F},
            					{0.80F, 0.80F, 0.80F},
            					{0.80F, 0.80F, 0.80F},
            					{0.80F, 0.80F, 0.80F},
            					{0.80F, 0.80F, 0.80F},
            					{0.80F, 0.80F, 0.80F},
            					{0.70F, 0.70F, 0.70F},
            					{0.70F, 0.70F, 0.70F},
            					{0.70F, 0.70F, 0.70F},
            					{0.70F, 0.70F, 0.70F},
            					{0.70F, 0.70F, 0.70F},
            					{0.70F, 0.70F, 0.70F},
            					{0.70F, 0.70F, 0.70F},
            					{0.70F, 0.70F, 0.70F},
            					{0.70F, 0.70F, 0.70F},
            					{0.60F, 0.10F, 1.00F},
            					{0.60F, 0.20F, 1.00F},
            					{0.60F, 0.30F, 1.00F},
            					{0.60F, 0.40F, 1.00F},
            					{0.60F, 0.50F, 1.00F},
            					{0.60F, 0.60F, 1.00F},
            					{0.60F, 0.70F, 1.00F},
            					{0.60F, 0.80F, 1.00F},
            					{0.60F, 0.90F, 1.00F},
            					{0.50F, 0.10F, 1.00F},
            					{0.50F, 0.20F, 1.00F},
            					{0.50F, 0.30F, 1.00F},
            					{0.50F, 0.40F, 1.00F},
            					{0.50F, 0.50F, 1.00F},
            					{0.50F, 0.60F, 1.00F},
            					{0.50F, 0.70F, 1.00F},
            					{0.50F, 0.80F, 1.00F},
            					{0.50F, 0.90F, 1.00F},
            					{0.40F, 0.10F, 1.00F},
            					{0.40F, 0.20F, 1.00F},
            					{0.40F, 0.30F, 1.00F},
            					{0.40F, 0.40F, 1.00F},
            					{0.40F, 0.50F, 1.00F},
            					{0.40F, 0.60F, 1.00F},
            					{0.40F, 0.70F, 1.00F},
            					{0.40F, 0.80F, 1.00F},
            					{0.40F, 0.90F, 1.00F},
            					{0.30F, 0.10F, 1.00F},
            					{0.30F, 0.20F, 1.00F},
            					{0.30F, 0.30F, 1.00F},
            					{0.30F, 0.40F, 1.00F},
            					{0.30F, 0.50F, 1.00F},
            					{0.30F, 0.60F, 1.00F},
            					{0.30F, 0.70F, 1.00F},
            					{0.30F, 0.80F, 1.00F},
            					{0.30F, 0.90F, 1.00F},
            					{0.20F, 0.10F, 1.00F},
            					{0.20F, 0.20F, 1.00F},
            					{0.20F, 0.30F, 1.00F},
            					{0.20F, 0.40F, 1.00F},
            					{0.20F, 0.50F, 1.00F},
            					{0.20F, 0.60F, 1.00F},
            					{0.20F, 0.70F, 1.00F},
            					{0.20F, 0.80F, 1.00F},
            					{0.20F, 0.90F, 1.00F},
            					{0.10F, 0.10F, 1.00F},
            					{0.10F, 0.20F, 1.00F},
            					{0.10F, 0.30F, 1.00F},
            					{0.10F, 0.40F, 1.00F},
            					{0.10F, 0.50F, 1.00F},
            					{0.10F, 0.60F, 1.00F},
            					{0.10F, 0.70F, 1.00F},
            					{0.10F, 0.80F, 1.00F},
            					{0.10F, 0.90F, 1.00F},
            					{0.00F, 0.10F, 1.00F},
            					{0.00F, 0.20F, 1.00F},
            					{0.00F, 0.30F, 1.00F},
            					{0.00F, 0.40F, 1.00F},
            					{0.00F, 0.50F, 1.00F},
            					{0.00F, 0.60F, 1.00F},
            					{0.00F, 0.70F, 1.00F},
            					{0.00F, 0.80F, 1.00F},
            					{0.00F, 0.90F, 1.00F},
            					{0.00F, 0.00F, 0.00F},
            					};
	*/
	protected float cmin = 0;
	protected float cmax = 0;
	protected static int clenght = 0;
	
	public static void GenerateColorMap()
	{
		map = new float[125][3];
		
		int i = 0;
		for(float r = 0.00F ; r < 1.00F; r+=0.01F)			
		{
			map[i][0] = r;
			map[i][1] = 1 - r;
			map[i][2] = r;
			i++;
		}
			
			
		clenght = 100;
	}
	
	public void setCMin(float min)
	{
		cmin = min;
	}
	
	public void setCMax(float max)
	{
		cmax = max;
	}
	
	public static void drawColorMap(Graphics2D g, int x, int y)
	{
		AffineTransform af = new AffineTransform();
		Rectangle2D.Double frame = new Rectangle2D.Double(x, y, 40, clenght * 4);
		
		g.draw(af.createTransformedShape(frame));
		int axisY = y;
		int i = 0;
		for(i = 0; i < clenght; i++)
		{
			if(i % 10 == 0)
			{
				g.setColor(Color.black);
				g.drawString(new Integer(i).toString(), x + 50, axisY + 5);
			}
			g.setColor(new Color(map[i][0], map[i][1], map[i][2]));
			g.draw(af.createTransformedShape(new Line2D.Double(x, new Double(axisY), x+40, new Double(axisY))));
			axisY++;
			g.draw(af.createTransformedShape(new Line2D.Double(x, new Double(axisY), x+40, new Double(axisY))));
			axisY++;
			g.draw(af.createTransformedShape(new Line2D.Double(x, new Double(axisY), x+40, new Double(axisY))));
			axisY++;
			g.draw(af.createTransformedShape(new Line2D.Double(x, new Double(axisY), x+40, new Double(axisY))));
			axisY++;
		}
		g.setColor(Color.black);
		g.drawString(new Integer(i).toString(), x + 50, axisY + 5);
	}
	
	public Color getColor(Double value)
	{
		int index = (int)Math.floor(((value.floatValue() - cmin)/(cmax - cmin)) * clenght);
		
		index = index % clenght;
		
		return new Color(map[index][0], map[index][1],map[index][2]);
	}
}
